﻿using System;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq.Expressions;
using System.Reflection;
using NiuX.Dapper.Attributes;
using NiuX.Dapper.Repositories;

namespace NiuX.Dapper;

public class DbQuerableContextModel<T> : DbQuerableContextModel, IDbQuerableContextModel<T>
{

    //public T Entity { get; set; }

    private string _tableName;

    public string TableName
    {
        get => _tableName.IsNullOrEmpty() ? GetAndSetTableName() : _tableName;
        set => _tableName = value;
    }

    public Type EntityType { get; set; } = typeof(T);


    public Expression SelectExpression { get; set; }

    public Expression<Func<T, bool>> WhereExpression { get; set; }


    public T UpdatingEntity { get; set; }

    public T InsertingEntity { get; set; }


    public string[] OrderByFields { get; set; }


    public bool IsDescending { get; set; }


    public string[] SelectedFields { get; set; }
    public int? Skip { get; set; }
    public int CompanyId { get; set; }
    public DatabaseType DatabaseType { get; set; }
    public int TakeCount { get; set; }

    public int GetTableId() => CompanyId % 10;

    public string GetAndSetTableName()
    {
        _tableName = EntityType.GetCustomAttribute<TableAttribute>()?.Name ?? EntityType.Name;

        if (EntityType.GetCustomAttribute<TableSplitableAttribute>() != null)
        {
            _tableName += GetTableId();
        }

        return _tableName;
    }


}


public class DbQuerableContextModel
{
    public string ConnectionString { get; set; }
}